package com.ctrip.business;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ctrip.constant.OrderStatusEnum;
import com.go.commons.dto.Page;
import com.go.commons.utils.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;

import java.util.HashMap;
import java.util.Map;

/**
 * https://vbooking.ctrip.com/VBKH5/
 * https://vbooking.ctrip.com/dingzhi/RobApp/GetRobRequireList?_fxpcqlniredt=09031124110231317451
 *
 {pageIndex: 1, pageSize: 5, orderType: "GrabOrder", isAll: false, contentType: "json"}
 contentType
 :
 "json"
 isAll
 :
 false
 orderType
 :
 "GrabOrder"
 pageIndex
 :
 1
 pageSize
 :
 5
 */
@Slf4j
public class PCOrderService {
    public static JSONArray listOrders(Page page, OrderStatusEnum orderStatusEnum)  {
        if (orderStatusEnum == null) {
            orderStatusEnum=OrderStatusEnum.WAITING;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("random", 68);
        params.put("pageIndex", page.getCurPage());
        params.put("pageSize", page.getPageSize());
        Map<String, Object> cond = new HashMap<>();
        cond.put("TravelNature", "A");
        cond.put("RequireID", "");
        cond.put("OrderType", "GrabOrder");
        cond.put("IsAll", false);
        cond.put("SortName", "");
        cond.put("Sorting", "ascending");
        cond.put("Version", 2);
        cond.put("OrderStatusType", orderStatusEnum.getType());//0全部 1待抢单 2被抢 3已抢 4已过期 5已取消
        params.put("condStr", JSON.toJSONString(cond));
        try {
            String response = HttpClientUtil.get("http://vbooking.ctrip.com/dingzhi/GrabRequire/GetRequireList", params, new Header[]{new BasicHeader("Cookie", UserService.cookie)});
            JSONObject jsonObject = JSON.parseObject(response).getJSONObject("Data");
            page.setTotalRow(jsonObject.getInteger("TotalCount"));
            JSONArray array= jsonObject.getJSONArray("Items");
            if (array != null) {
                return array;
            }
        } catch (Exception e) {
            log.error(e.toString(),e);
        }
        return new JSONArray();
    }

    /**
     <pre>
     var obj = $("tr_" + 0);
     var requireDetailID = "1253285";
     var platformUserID = "c0f46bf5-74e5-4b57-8747-7b85fcb67573";
     var platformProviderID = "36823";
     var requireID = "4097935399";
     confirmGrabOrder(obj, requireDetailID, platformUserID, platformProviderID, requireID);
     </pre>
     * @param order
     * @return
     */
    public static boolean grabOrder(JSONObject order){
        String requireDetailID=order.getString("RequireDetailID");
        String platformUserID=order.getString("PlatformUserID");
        String platformProviderID=order.getString("PlatformProviderID");
        String orderID=order.getString("OrderID");

        return true;
    }
}
